﻿MPT.addAction("friend_visit", function(_elm) {
    var friend_list_jQ = $('.e_friend_list', _elm);
    var my_list_jQ = $('.e_my_list', _elm);
    var title_jQ = $('.e_title', _elm);
    var visit_u_jQ = $('.e_visit_u', _elm);
    var visit_me_jQ = $('.e_visit_me', _elm);
    var page_jQ = $('.e_page', _elm);
    var type_jQ = 0;

    var initialize = function() {

        Tab.switchFriend();
        
        uVisit(1,21);

        // 谁来看过我
        visit_u_jQ.click(function() {
            visit_me_jQ.removeClass('active');
            $(this).addClass('active');
            title_jQ.html('最近访问过您的用户：');
            friend_list_jQ.show();
            my_list_jQ.hide();
            type_jQ = 0;
            page_jQ.empty();
            friend_list_jQ.empty();
            uVisit(1,21);
        });

        // 我去看过谁
        visit_me_jQ.click(function() {
            visit_u_jQ.removeClass('active');
            $(this).addClass('active');
            title_jQ.html('最近您访问过的用户：');
            friend_list_jQ.hide();
            my_list_jQ.show();
            type_jQ = 1;
            page_jQ.empty();
            my_list_jQ.empty();
            myVisit(1,21);
        });
    }

    // 添加事件
    var addActionGroup = function(_group_elm, _data) {
        var friend_jQ = $('.e_friend', _group_elm);
        var attention_jQ = $('.e_attention', _group_elm);
        var delete_jQ = $('.e_delete', _group_elm);

        var uinfo = { "uid": _data.visitorId, "nickname": _data.nickName };

        //移动上去背景变色
        $(_group_elm).mouseover(function() {
            $(this).addClass('active');
        }).mouseout(function() {
            $(this).removeClass('active');
        });

        // 加好友
        friend_jQ.click(function() {
            var data = { "targetID": _data.uid, "gids": 0 };
            if (!_data.friend) {
                SocialUtil.displayAddDlg(uinfo, function() {
                    friend_jQ.unbind('click');
                });
            }
        });

        // 加关注
        attention_jQ.click(function() {
            var data = { "targetID": _data.visitorId, "gids": 0 };
            if (!_data.followed) {
                Backend.follow(data, function(_resp) {
                    if (Backend.resultValidate(_resp)) {
                        attention_jQ.addClass('in_attention').removeClass('add_attention').html('已关注').attr('title', '加关注').unbind('click');
                    } else {
                        Tip.show("添加关注失败");
                    }
                }, function() {
                    Tip.show("添加关注失败");
                });
            };
        });

        // 删脚印
        delete_jQ.click(function() {
            var data = { "visitor": _data.visitorId + "|" + _data.nickName, "type": type_jQ };
            Backend.deleteVisitRecords(data, function(_resp) {
                if (Backend.resultValidate(_resp)) {
                    _group_elm.fadeOut("fast", function() { $(this).remove(); })
                } else {
                    Tip.show("删除脚印失败");
                }
            }, function() {
                Tip.show("删除脚印失败");
            });
        });
    }

    var uVisit = function(pageNo,pageSize) {
        var data = { "pageNo": 1, "type": 0, "pageSize":pageSize  };
        Backend.findVisitRecords(data, function(_resp) {

            if (Backend.pageBeanValidate(_resp)) {
                var results = _resp.results;
                $.each(results, function(_i, _d) {
                    var msg_jQ = $(MPT.getTmpl('one_user', _d));
                    addActionGroup(msg_jQ, _d);
                    friend_list_jQ.append(msg_jQ);
                });
                //显示分页
                var page_param = { "pageNo": _resp.pageNo, "pageSize": _resp.pageSize, "totalRecords": _resp.totalRecoreds, "totalPages": _resp.totalPages };
                var pageBar_jQ = (new Paginate).createPageBar(page_param, function(_click_page) {
                    my_list_jQ.empty();
                    myVisit(_click_page,21);
                });
                page_jQ.empty().append(pageBar_jQ);
            } else {
                friend_list_jQ.html("还没有任何人访问你");
            }
        }, function() {
            friend_list_jQ.html("数据加载错误");
        });
    };

    var myVisit = function(pageNo,pageSize) {
        var data = { "pageNo": pageNo, "type": 1, "pageSize":pageSize };
        Backend.findVisitRecords(data, function(_resp) {

            if (Backend.pageBeanValidate(_resp)) {
                var results = _resp.results;
                $.each(results, function(_i, _d) {
                    var msg_jQ = $(MPT.getTmpl('one_user', _d));
                    addActionGroup(msg_jQ, _d);
                    my_list_jQ.append(msg_jQ);
                });

                //显示分页
                var page_param = { "pageNo": _resp.pageNo, "pageSize": _resp.pageSize, "totalRecords": _resp.totalRecoreds, "totalPages": _resp.totalPages };
                var pageBar_jQ = (new Paginate).createPageBar(page_param, function(_click_page) {
                    my_list_jQ.empty();
                    myVisit(_click_page,21);
                });
                page_jQ.empty().append(pageBar_jQ);

            } else {
                my_list_jQ.html("你还没有访问任何人");
            }

        }, function() {
            my_list_jQ.html("数据加载错误");
        });
    }

    initialize();
});